﻿using System;

using System.Data;
using System.Data.SqlClient;
using System.Text;

namespace IFD2
{
    public partial class AvailFlights : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Application["DEBUG"].ToString() != "1")
            {
                if (IsRobot.Test("Query", 1, 60, 6))
                {
                    lblError.Text = "Please don't query continually!";
                    return;
                }
            }
            if (IsPostBack)
            {
                lblError.Text = "";
                return;
            }
            else
            {
                if (Session["ACL"].ToString().IndexOf("SA3") < 0)
                {
                    Response.Redirect("unauthorized.aspx");
                    return;
                }
            }

            int trip_count,/*max_opn,bkd,blk,*/adt, chd, inf/*,avail*/;

            //if (Request.Form["dlTripCount"] == null)
            //{
            //    butNext.Enabled = false;
            //    lblError.Text = "Illegal Post Url!";
            //    return;
            //}
            if (Session["TRIP_COUNT"] == null)
            {
                lblError.Text = "Session[TRIP_COUNT] not found!";
                return;
            }
            trip_count = Convert.ToInt16(Session["TRIP_COUNT"].ToString());
            //adt = Convert.ToInt16(Session["ADT_COUNT"].ToString());
            //chd = Convert.ToInt16(Session["CHD_COUNT"].ToString());
            //inf = Convert.ToInt16(Session["INF_COUNT"].ToString());
            //if ( adt < 0 || chd < 0 || inf < 0 || (adt<inf) || adt+chd>50 )
            //{
            //    lblError.Text = "Illegal Passenger Count!";
            //    butNext.Enabled = false;
            //    return;
            //}

            //Session["SEATS"] = adt + chd;

            //string[] ori/*trips*/ = new string[trip_count + 1];
            //string[] des = new string[trip_count + 1];
            //string[] trip_dates = new string[trip_count];
            StringBuilder buf = new StringBuilder();
            buf.Clear();
            buf.Append("<table style='min-width:800px;width:100%;'><tr><td>");
            //DateTime last_date = Convert.ToDateTime("1999-12-31");
            hidSegCount.Value = trip_count.ToString();
            for (int i = 1; i <= trip_count; i++)
            {
                buf.Append("<div id=\"seg_" + i.ToString() + "\"></div><br/>");
            }
            buf.Append("</td></tr></table>");
            litSegments.Text = buf.ToString();
        }

        protected void QueryFlights(int i)// 'i' is segment_sn.
        {
            return;//已改用ajax查询航班，此处无效！！！！！！！！！！！！！！

            SqlCommand cmd = new SqlCommand();
            SqlParameter para;//,para2;

            DataTable dt_flights = new DataTable(), /*dt_cabins = new DataTable(), dt_classes = new DataTable(),*/ dt_fares = new DataTable();//,dt_dcp =new DataTable();

            //TableRow tabRow = new TableRow();
            //TableCell tabCell;
            StringBuilder tabBuf = new StringBuilder();

            int ret_code;

            //tabRow = new TableRow();
            //tabCell = new TableCell();

            //tabCell.Text = i.ToString() + ". " + Convert.ToDateTime(Session["FLT_DATE_" + i.ToString()]).ToShortDateString();
            tabBuf.Append("<b>" + i.ToString() + ". " + Session["ORI_" + i.ToString()].ToString() + "-" + Session["DES_" + i.ToString()].ToString() + " " + Convert.ToDateTime(Session["FLT_DATE_" + i.ToString()]).ToShortDateString() + "</b>");

            //tabCell.ColumnSpan = 5;
            //tabCell.Font.Bold = true;
            //tabRow.Cells.Add(tabCell);
            //switch (i)
            //{
            //    case 1:
            //        tabTrip1.Rows.AddAt(0, tabRow);
            //        break;
            //    case 2:
            //        tabTrip2.Rows.AddAt(0, tabRow);
            //        break;
            //    case 3:
            //        tabTrip3.Rows.AddAt(0, tabRow);
            //        break;
            //    case 4:
            //        tabTrip4.Rows.AddAt(0, tabRow);
            //        break;
            //}
            try
            {
                cmd.Parameters.Clear();
                cmd.CommandText = "bll_query_get_avail_flts";
                para = cmd.Parameters.AddWithValue("@ori_city", Session["ORI_" + i.ToString()]);
                para = cmd.Parameters.AddWithValue("@des_city", Session["DES_" + i.ToString()]);
                para = cmd.Parameters.AddWithValue("@lang", Session["LANG"].ToString());
                para = cmd.Parameters.AddWithValue("@flt_date", Convert.ToDateTime(Session["FLT_DATE_" + i.ToString()]));
                //para = cmd.Parameters.AddWithValue("@seats", Convert.ToInt16(Session["SEATS"]));
                //para = cmd.Parameters.AddWithValue("@seg_sn", i);
                //para = cmd.Parameters.AddWithValue("@seg_total", Convert.ToInt16(Session["TRIP_COUNT"]));
                para = cmd.Parameters.AddWithValue("@airline", Session["AIRLINE"].ToString());
                para = cmd.Parameters.AddWithValue("@office", Session["OFFICE"].ToString());
                para = cmd.Parameters.AddWithValue("@user", Session["USER"].ToString());
                para = cmd.Parameters.AddWithValue("@ip_addr", Request.UserHostAddress.ToString());

                ret_code = TinyDAL.Exec(ref cmd, ref dt_flights);
                if (ret_code != 0)
                {
                    if (ret_code == 1)
                        lblError.Text = i.ToString() + ":Origin city not found!";
                    else if (ret_code == 2)
                        lblError.Text = i.ToString() + ":Daylight saving time error!";
                    else
                        lblError.Text = "Segment " + i.ToString() + " Error:" + ret_code.ToString();
                    return;
                }

                StringBuilder tmp = new StringBuilder();
                int row_count = 0;
                foreach (DataRow row_flights in dt_flights.Rows)
                {
                    row_count++;
                    tmp.Append("flt_avail_fares.aspx?airline=" + row_flights["airline"].ToString() + "&flt_no=" + row_flights["flt_no"].ToString() + "&flt_date="
                        + row_flights["flt_date"].ToString() + "&ori=" + row_flights["ori_city"].ToString() + "&ori_airport=" + row_flights["ori_airport"].ToString()
                        + "&des=" + row_flights["des_city"].ToString() + "&des_airport=" + row_flights["des_airport"].ToString() + "&dep_time=" + row_flights["dep_time"].ToString() + "&arr_time=" + row_flights["arr_time"].ToString()
                        + "&lang=" + Session["LANG"].ToString() + "&seats=" + Session["SEATS"].ToString() + "&tax_type=" + row_flights["tax_type"].ToString() + "&international=" + row_flights["international"].ToString());
                    tmp.Append("&seg_sn=" + i.ToString() + "&seg_total=" + Session["TRIP_COUNT"].ToString());
                    tabBuf.Append("<table><tr><td width='10px'></td><td><button type='button' id='btn_" + i.ToString() + "_" + row_count.ToString() + "' onMouseOver=\"QFltAvailFares('" + i.ToString() + "','" + row_count.ToString() + "','" + tmp.ToString() + "');\">></button></td><td width='50px'><b>"
                        + Convert.ToDateTime(row_flights["dep_time"]).ToShortTimeString() + "</b></td><td width='50px'>" + Convert.ToDateTime(row_flights["arr_time"]).ToShortTimeString() + "</td><td width='80px'>"
                        + "<b>" + row_flights["airline"].ToString() + row_flights["flt_no"].ToString() + "</b> ");
                    if (row_flights["direct_fly"].ToString() == "0")
                        tabBuf.Append("(Stop)");
                    tabBuf.Append("</td><td width='160px'>" + row_flights["airline_name"].ToString() + "</td><td width='50px'>" + row_flights["ac_type"].ToString() + "</td><td>" + row_flights["ori_apt_name"] + "(" + row_flights["ori_terminal"] + ") - " + row_flights["des_apt_name"] + "(" + row_flights["des_terminal"] + ")</td></tr></table>");

                    tabBuf.Append("<div id=\"flt_" + i.ToString() + "_" + row_count.ToString() + "\"></div><br/>");

                    tmp.Clear();
                }
                dt_flights.Clear();

            }
            catch (Exception ex)
            {
                if (Application["DEBUG"].ToString() == "1")
                    lblError.Text = i.ToString() + ":" + ex.Message;
                else
                    lblError.Text = i.ToString() + ": Unexpected error! Please recheck the input/output data first. If the error appears again, contact adminstrator with your operating details please.";
                butNext.Enabled = false;
            }
        }
        protected void butNext_Click(object sender, EventArgs e)
        {
            ////检查衔接时间是否足够，之后才转下页！
            ////衔接检查应该放在sp中！国内、国际衔接时间不同！
            //int trip_count, i;
            //bool TransferSuccess = true;
            //DateTime DepTime, LastArrTime;

            //TimeSpan span = new TimeSpan(2, 0, 0);//2 hours
            //trip_count = Convert.ToInt16(Session["TRIP_COUNT"].ToString());
            //string key;
            //string[] strArray;
            //LastArrTime = Convert.ToDateTime("1900-1-1");
            //for (i = 1; i <= trip_count; i++)
            //{
            //    if (Request.Form["KEY" + i.ToString()] == null)
            //    {
            //        lblError.Text = "Please select a record.";
            //        return;
            //    }
            //    key = Request.Form["KEY" + i.ToString()];
            //    strArray = key.Split('|');
            //    if (strArray[4] == "-")//OPEN
            //    {
            //        DepTime = Convert.ToDateTime("1900-1-1");
            //    }
            //    else
            //    {
            //        DepTime = Convert.ToDateTime(strArray[4]);
            //    }
            //    if (DepTime - LastArrTime < span)
            //    {
            //        TransferSuccess = false;
            //        break;
            //    }
            //    if (strArray[5] == "-")//OPEN
            //    {
            //        LastArrTime = Convert.ToDateTime("1900-1-1");
            //    }
            //    else
            //    {
            //        LastArrTime = Convert.ToDateTime(strArray[5]);
            //    }
            //}
            //if (!TransferSuccess)
            //{
            //    //测试！！会停止转到fare_avail.aspx吗？不能！！
            //    lblError.Text = "航班衔接时间不够!";
            //    return;
            //}

        }

        
    }
}